<!--_meta 作为公共模版分离出去-->
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="Bookmark" href="/favicon.ico" >
    <link rel="Shortcut Icon" href="/favicon.ico" />
    <!--[if lt IE 9]>
    <script type="text/javascript" src="../js/html5shiv.js"></script>
    <script type="text/javascript" src="../js/respond.min.js"></script>
    <![endif]-->
    <link href="../css/H-ui.min.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../css/H-ui.css" />
    <link rel="stylesheet" type="text/css" href="../css/Hui-iconfont/1.0.8/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="../css/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <title></title>
</head>
<body>
<article class="page-container">
    <form action="" method="post" class="form form-horizontal" id="form-admin-role-add">
        <input type="hidden" id="roleId" data-roleid=""/>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>角色名称：</label>
            <div class="formControls col-xs-8 col-sm-10">
                <input type="text" class="input-text" value="" placeholder="" id="roleName" name="name">
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2">权限模块：</label>
            <div class="formControls col-xs-8 col-sm-10 checkBoxCon">

            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                <button class="btn btn-success radius submit" name=""><i class="icon-ok"></i> 确定</button>
            </div>
        </div>
    </form>
</article>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="/js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="/js/layer/2.4/layer.js"></script>
<script type="text/javascript" src="/js/H-ui.js"></script>
<script type="text/javascript" src="/js/H-ui.min.js"></script>
<script type="text/javascript" src="/js/postJson.js"></script>
<script type="text/javascript" src="/js/root.js"></script>
<!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<style>
    em{
        font-style: normal;
    }
</style>
<script type="text/javascript">
    //获取父窗口roleId
    var roleId =  Number(parent.$("#roleId").attr("data-roleid"))
    $("#roleId").attr("data-roleid",roleId)
    var url = "/sysRole/byRoleId";
    $.ajax({
        url:url+"/"+roleId,
        type:"get",
        async:false,
        success:function(msg){
            // console.log(JSON.stringify(msg))
//            alert(JSON.stringify(msg.data.list[0].operateId))
            $("#roleName").val(msg.data.name)
            var htmlString = '';
           var arr =msg.data.operates;
            $.each(msg.data.list,function(i,n){
                if(n.parentId==0){
                 htmlString+='<dl class="permission-list">'
                 htmlString+='<dt>'
                 htmlString+='<label>'
                 htmlString+='<input type="checkbox" value="" name="" operateId="'+ n.id+'">'
                 htmlString+= n.name+'</label>'
                 htmlString+='</dt>'
                 htmlString+='<dd>'
                 htmlString+='<dl class="cl permission-list2">'
                 htmlString+='<dd>'
                 $.each(msg.data.list,function(i2,n2){
                     if(n.id==n2.parentId){
                         htmlString+='<label class="">'
                         htmlString+='<input type="checkbox" value="" name="" operateId="'+n2.id+'">'
                         htmlString+=n2.name+'</label>'
                         htmlString+='<em class="null">'+"（"+'</em>'
                         $.each(msg.data.listByType,function(i3,n3){
                             if(n2.id==n3.parentId){
                                 htmlString+='<label class="">'
                                 htmlString+='<input type="checkbox" value="" name="" operateId="'+n3.id+'" parentId="'+n3.parentId+'">'
                                 htmlString+=n3.name+'</label>'
                             }

                         })
                         htmlString+='<em class="null2">'+"）"+'</em>'

                     }
                 })
                 htmlString+='</dd>'
                 htmlString+='</dl>'
                 htmlString+='</dd>'
                 htmlString+='</dl>'
                 }
            })
            $(".checkBoxCon ").append(htmlString);
            $("input[type=checkbox]").each(function(){
                for(var i = 0;i<arr.length;i++){
                    if($(this).attr("operateid")==arr[i]){
                        $(this).prop("checked",true)
                    }
                }
            })
//            console.log($(".null").length)
            $(".null").each(function(){
                //console.log($(this).next().hasClass("null2"))
                if($(this).next().hasClass("null2")==true){
                    $(this).addClass("hide")
                    $(this).next().addClass("hide")
                }
            })
            /*$(".permission-list2 dd").find("em").each(function(){
                console.log($(".null").next(".null2"))
                if($(".null").next(".null2")==true){
                    $(".null").hide()
                    $(".null2").hide()
               }
            })*/


        },
        error:function(XMLHttpRequest,textStatus,errorThrown){
            var XmlStatus = XMLHttpRequest.status
//                         console.log(XMLHttpRequest.status)
            if(XmlStatus==401){
                alert(XmlError.error401)
                parent.window.location.href = "../login.html"
            }
            if(XmlStatus==403){
                alert(XmlError.error403)
            }
        }
    });


    $(".permission-list dt input:checkbox").click(function(){
        $(this).closest("dl").find("dd input:checkbox").prop("checked",$(this).prop("checked"));
    });
    $(".permission-list2 dd input:checkbox").click(function(){
        if($(this).prop("checked")){
            $(this).closest("dl").find("dt input:checkbox").prop("checked",true);
            $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",true);
            var parentId = $(this).attr("parentId");
            $(".permission-list2 dd input[operateid='"+parentId+"']").prop("checked", true);
        }
        else{
            var operateid = $(this).attr("operateid");
            $(".permission-list2 dd input[parentid='"+operateid+"']").prop("checked", false);
            var onCheck =$(this).parent().parent().find("input:checked").length;
            var notCheck=$(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
            if(onCheck==0){
                $(this).closest("dl").find("dt input:checkbox").prop("checked",false);
            }
            if(notCheck==0){
                $(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",false);
            }
        }
    });

    $(".submit").bind("click",function(){
        var roleId = $("#roleId").attr("data-roleid")
        var roleName = $("#roleName").val();
        if(roleName==''){
            layer.msg("角色名称不能为空！",{icon:2,time:5000});
            $("#roleName").focus()
            return false;
        }else{
            var arr= {
                role:{"name":roleName,"id":roleId},
                "operates":[]
            }
            var url = "/sysRole/updateRole";
            $("input:checked").each(function(){
                arr.operates.push($(this).attr("operateId"))
            })
            var arrPost = JSON.stringify(arr)
            $.ajax({
                url:url,
                contentType:"application/json",
                data:arrPost,
                type:"post",
                async:false,
                dataType:'json',
                success:function(msg){
                    if(msg.success) {
                        parent.layer.msg("修改成功！",{icon:1,time:3000})
                    }else {
                        var error = msg.errorMessage;
                        parent.layer.msg(error,{icon:2,time:3000});
                    }
                },
                error:function(XMLHttpRequest,textStatus,errorThrown){
                    var XmlStatus = XMLHttpRequest.status
//                         console.log(XMLHttpRequest.status)
                    if(XmlStatus==401){
                        alert(XmlError.error401)
                        parent.window.location.href = "../login.html"
                    }
                    if(XmlStatus==403){
                        alert(XmlError.error403)
                    }
                }
            });
            var index = parent.layer.getFrameIndex(window.name);
            parent.$(".tableCon").find("tr").each(function(){
                if($(this).attr("roleid")==roleId){
                    $(this).find("td:first").text(roleName)
                }
            })
            //parent.layer.msg("添加成功！",{icon:1,time:1000})
            parent.layer.close(index);
        }

    })
</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>